package ar.org.cfired.creditos.gui.action;

import ar.org.cfired.creditos.gui.Contexto;
import ar.org.cfired.creditos.gui.Sesion;
import ar.org.cfired.hibernate.utils.HibernateUtil;
import ar.org.cfired.creditos.parametros.EstadosLog;
import java.math.BigInteger;
import java.util.Map;

import javax.servlet.ServletContext;

import ar.org.cfired.creditos.parametros.GuardarParametrosException;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;


public class RechazarAction extends Action {
	private ExecutionService executionService;
	private ProcessEngine processEngine;
	private Logger objetoLogger = Logger.getLogger(this.getClass().getName());
	
	public void execute(Sesion sesion) throws GuardarParametrosException {
		Session hibSession = null;
		try {
			hibSession = HibernateUtil.getSessionFactory().getCurrentSession();		
			hibSession.beginTransaction();
			
			EstadosLog estado = new EstadosLog();
				estado.setLogTask(BigInteger.valueOf(Integer.valueOf(sesion.getTareaActiva().getTaskID())));
				estado.setLogUsuario(sesion.getUsuario());
				estado.setLogExecutionId((sesion.getTareaActiva().getExecutionID()==null?"a":sesion.getTareaActiva().getExecutionID()));
				estado.setEstId("RE");

			hibSession.save(estado);
			hibSession.getTransaction().commit();
		}
		catch (HibernateException e) {
			if (hibSession.getTransaction() != null)
				if (hibSession.getTransaction().isActive())
					hibSession.getTransaction().rollback();
			e.printStackTrace();
			throw new GuardarParametrosException(); 
		}
		catch (Exception e) {
			if (hibSession.getTransaction() != null)
				if (hibSession.getTransaction().isActive())
					hibSession.getTransaction().rollback();
			e.printStackTrace();
			throw new GuardarParametrosException();
		}
	}
	
	
	public void execute(String taskID, String usuario, String executionID) throws GuardarParametrosException {
		Session hibSession = null;
		try {
			hibSession = HibernateUtil.getSessionFactory().getCurrentSession();		
			hibSession.beginTransaction();
			
			String instance = executionID.substring(executionID.indexOf(".")+1);
			if (instance.contains("."))
			instance = instance.substring(0,instance.indexOf("."));
			
			
			
			
			EstadosLog estado = new EstadosLog();
			//objetoLogger.debug(BigInteger.valueOf(Integer.valueOf(taskID)));
				estado.setLogTask(BigInteger.valueOf(Integer.valueOf(taskID).longValue()));
				estado.setLogUsuario(usuario);
				estado.setLogExecutionId((executionID.equals("")?"a":executionID));
				estado.setLogInstance(BigInteger.valueOf(Integer.valueOf(instance).longValue()));
				estado.setEstId("RE");

				hibSession.save(estado);
		
				System.out.println("EXECUTION ID : " + executionID);
				if(executionService == null)
					System.out.println("executionService es NULL");
				
			Map<String, Object> variables = executionService.getVariables(executionID, executionService.getVariableNames(executionID));
			//variables.put("usuarioModificacion", variables.getvariable("",""));
			variables.put("executionID", executionID);
			
			System.out.println("OK");
			
			ProcessInstance modificacion = executionService.startProcessInstanceByKey("Rechazados", variables);			
			
			
			hibSession.getTransaction().commit();
		}
		catch (HibernateException e) {
			if (hibSession.getTransaction() != null)
				if (hibSession.getTransaction().isActive())
					hibSession.getTransaction().rollback();
			e.printStackTrace();
			throw new GuardarParametrosException(); 
		}
		catch (Exception e) {
			if (hibSession.getTransaction() != null)
				if (hibSession.getTransaction().isActive())
					hibSession.getTransaction().rollback();
			e.printStackTrace();
			throw new GuardarParametrosException();
		}
	}

	public void setProcessEngine(ProcessEngine pe) {
		processEngine = pe;
	}
	
	public void setExecutionService(ExecutionService es) {
		executionService = es;
	}

	
	@Override
	public void execute(ExecutionService execution, String taskID, String usuario, String executionID) throws GuardarParametrosException {		
		executionService = execution;
		this.execute(taskID, usuario, executionID);
	}	

}